package weaver.interfaces.jhthome.duss.action;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author dusongsong
 * @date 2023年08月11日 10:07
 */
public class JhthomeUtil extends BaseBean {
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        writeLog("-----WorkflowCreateByRequestMap--------");
        writeLog("creatorId:" + creatorId);
        writeLog("workflowId:" + workflowId);
        writeLog("requestName:" + requestName);
        writeLog("IsNextFlow:" + IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);
        requestInfo.setWorkflowid(workflowId);
        requestInfo.set_Remark("");
        requestInfo.setDescription(requestName);
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(weaver.general.Util.null2String(entry.getKey()));
            propertyArray[p].setValue(weaver.general.Util.null2String(entry.getValue()));
            writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue());
            p++;
        }
        writeLog("-----p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            try {
                requestid = service.createRequest(requestInfo);
            } catch (Exception e) {
                writeLog("=======" + e.getMessage());
            }
            StringBuffer sbf = new StringBuffer("\r\n-----log begin");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----name:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----time:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----createWf:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----request:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----request:" + requestid);
            sbf.append("\r\n-----create end");
            writeLog(sbf.toString());
            System.out.println("" + sbf.toString());
        } catch (Exception e) {
            writeLog("err��" + e);
            System.out.println("err��" + e);
        }
        return requestid;
    }

    public boolean updateDetailData(String tablename, Map<String, Object> map) {
        String insertsql = "insert into " + tablename + "";
        String keyString = "";
        String valueString = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (map.get(key) == null || "".equals(map.get(key).toString())) {
                continue;
            }
            keyString += key + ",";
            valueString += "'" + map.get(key).toString() + "',";
        }
        if (keyString.endsWith(",")) {
            keyString = keyString.substring(0, keyString.length() - 1);
        }
        if (valueString.endsWith(",")) {
            valueString = valueString.substring(0, valueString.length() - 1);
        }
        insertsql += "(" + keyString + ") values(" + valueString + ")";
        writeLog(">>>>>updateDetailData>>>>insertsql=" + insertsql);
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(insertsql);
    }

    public String getIdByRequestId(String tableName, String requestid) {
        RecordSet rs = new RecordSet();
        rs.execute("select id from " + tableName + " where requestid='" + requestid + "'");
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public void newModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    public int insertModelData(int modeid, int userid, Map<String, Object> map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=" + modeid;
        rs.execute(sql);
        rs.next();
        String tablename = Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)" +
                "values('" + uuid + "','" + userid + "','" + TimeUtil.getCurrentDateString() + "','" + TimeUtil.getOnlyCurrentTimeString() + "','" + modeid + "')";
        if (rs.execute(insSql)) {
            rs1.execute("select id from " + tablename + " where uuid='" + uuid + "'");
            rs1.next();
            dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get(key).toString() + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate(updatesql);
                }
                newModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }

    public String getBjcs(String requestid) {
        RecordSet rs = new RecordSet();
        rs.execute("select max(bjcs) from uf_wlxjdtz where requestid=" + requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public String getBjrByDtl2(String tablename, String mainid) {
        RecordSet rs = new RecordSet();
        String resourceid = "";
        rs.execute("select bjr from " + tablename + "_dt2 where mainid=" + mainid + " group by bjr");
        while (rs.next()) {
            String bjr = Util.null2String(rs.getString("bjr"));
            resourceid += "".equals(resourceid) ? bjr : "," + bjr;
        }
        return resourceid;
    }


}
